package gui;

import java.awt.Color;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import mappers.LoginMapper;
import domains.Login;

public class LoginGUI implements ActionListener, KeyListener {
	private JFrame loginFrame;
	private JTextField usernameTF;
	private JPasswordField passwordTF;
	private JLabel titleLbl, usernameLbl, passwordLbl;
	private Font font;
	private JPanel panel;
	private GridLayout grid;
	private JButton loginBtn, linkBtn;
	private URI uri;

	public LoginGUI() {
		font = new Font("arial", Font.PLAIN, 30);
		grid = new GridLayout(2,2);
		panel = new JPanel();
		
		titleLbl = new JLabel("Login");
		titleLbl.setBounds(150, 20, 100, 40);
		titleLbl.setFont(font);
		
		usernameLbl = new JLabel("Brugernavn:");	
		usernameTF = new JTextField();
		usernameTF.addKeyListener(this);
		
		passwordLbl = new JLabel("Password:");
		passwordTF = new JPasswordField();
		passwordTF.addKeyListener(this);
		
		panel.setLayout(grid);
		
		panel.add(usernameLbl);
		panel.add(usernameTF);
		panel.add(passwordLbl);
		panel.add(passwordTF);			
		panel.setBounds(30, 100, 220, 50);
		
		loginBtn = new JButton("Login");
		loginBtn.setBounds(135, 190, 100, 25);
		
		loginBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {		
				login();
			}			
		});
		
		linkBtn = new JButton();
		linkBtn.setText("Glemt dit password?");
		linkBtn.setHorizontalAlignment(SwingConstants.LEFT);
		linkBtn.setOpaque(false);
		linkBtn.setBackground(Color.WHITE);
		linkBtn.addActionListener(this);
		linkBtn.setBorderPainted(false);
		linkBtn.setBounds(122, 155, 200, 25);
	
	}

	public void showGUI() {
		loginFrame = new JFrame();
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
		int w = loginFrame.getSize().width;
		int h = loginFrame.getSize().height;
		int x = (dim.width-w)/4;
		int y = (dim.height-h)/4;
		loginFrame.setTitle("Login");
		loginFrame.setLayout(null);
		loginFrame.setLocation(x, y);	
		loginFrame.add(titleLbl);
		loginFrame.add(panel);
		loginFrame.add(loginBtn);
		loginFrame.add(linkBtn);
		loginFrame.setResizable(false);
		loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		loginFrame.setSize(400, 300);
		loginFrame.setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e){
		JOptionPane.showMessageDialog(null,
				"Denne funktion bliver implementeret i næste version!");
		/*try {
			uri = new URI("http://google.com");
		} catch (URISyntaxException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		open(uri);	*/	
	}
	
	private static void open(URI uri) {
		
		if(Desktop.isDesktopSupported()) {
			try{
				Desktop.getDesktop().browse(uri);
				
			} catch(IOException e) {
				e.printStackTrace();
			}
		
		} else {
				
		}
	}
	

		public void keyPressed(KeyEvent e) {
			if(e.getKeyCode() == KeyEvent.VK_ENTER) {
				login();
			}
		}

		@Override
		public void keyReleased(KeyEvent e) {
			
		}

		@Override
		public void keyTyped(KeyEvent e) {
			
		}
		
		public void login() {
			LoginMapper lMapper = new LoginMapper();
			
			String username = usernameTF.getText(); 
			char[] pass = passwordTF.getPassword();
			String passString = new String(pass);
			Login l = new Login(username, passString);
			if(lMapper.checkLogin(l)) {
				loginFrame.setVisible(false);
				MainWindowGUI mGUI = new MainWindowGUI();
				mGUI.showGUI();
			} else {
				JOptionPane.showMessageDialog(null, "Det indtastede brugernavn og password stemmer ikke overens.");
			}
			
		}
}
